package org.op;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import org.op.util.MessageController;

public class FrameViewImpl implements FrameView {
	
	private Component component; // instantiate via Spring
	
	private ComponentView opMenuBarView;
	private ComponentView opToolBarView;
	private ComponentView opEditorPanelView;
	private ComponentView opNavPanelView;
	private ComponentView opStatusBarView;		
	
	private MessageController messageController;
	
	public FrameViewImpl() {
	}

	/**
	 * 
	 *  Build and attach all subcomponents
	 * 
	 */
	public void build() {
		
        // TODO - make this configurable
		JFrame frame = new JFrame();
		
        frame.setName("Wikibook");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        
		opMenuBarView.build();
		frame.setJMenuBar((JMenuBar) opMenuBarView.getComponent());
		
		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		frame.setContentPane(contentPanel);
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

		splitPane.setDividerLocation(150);
		
		opToolBarView.build();
		contentPanel.add(opToolBarView.getComponent(), BorderLayout.NORTH);
		
		opStatusBarView.build();
		contentPanel.add(opStatusBarView.getComponent(), BorderLayout.SOUTH);
		
		opNavPanelView.build();
		//splitPane.setLeftComponent(opNavPanelView.getComponent());
		//splitPane.add(opNavPanelView.getComponent());
		splitPane.setLeftComponent(opNavPanelView.getComponent());
		
		opEditorPanelView.build();
		splitPane.setRightComponent(opEditorPanelView.getComponent());
		
		contentPanel.add(splitPane, BorderLayout.CENTER);

		frame.setVisible(true);
		
		component = frame;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void showExceptionDialog(String message) {
		if (message==null) {
			message = messageController.getMessage("generic.error.dialog");
		}
		JOptionPane.showMessageDialog(component, message, "Error", JOptionPane.ERROR_MESSAGE);
	}
	
	public void showInfoDialog(String message) {
		JOptionPane.showMessageDialog(component, message, "Information", JOptionPane.INFORMATION_MESSAGE);
	}
	
	public JFrame getFrame() {
		return (JFrame)component;
	}
	
	public Component getComponent() {
		return component;
	}

	public void setComponent(Component component) {
		this.component = component;
	}

	public ComponentView getOpMenuBarView() {
		return opMenuBarView;
	}

	public void setOpMenuBarView(ComponentView opMenuBarView) {
		this.opMenuBarView = opMenuBarView;
	}

	public ComponentView getOpToolBarView() {
		return opToolBarView;
	}

	public void setOpToolBarView(ComponentView opToolBarView) {
		this.opToolBarView = opToolBarView;
	}

	public ComponentView getOpEditorPanelView() {
		return opEditorPanelView;
	}

	public void setOpEditorPanelView(ComponentView opEditorPanelView) {
		this.opEditorPanelView = opEditorPanelView;
	}

	public ComponentView getOpNavPanelView() {
		return opNavPanelView;
	}

	public void setOpNavPanelView(ComponentView opNavPanelView) {
		this.opNavPanelView = opNavPanelView;
	}

	public ComponentView getOpStatusBarView() {
		return opStatusBarView;
	}

	public void setOpStatusBarView(ComponentView opStatusBarView) {
		this.opStatusBarView = opStatusBarView;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}


}
